//
//  Ank_musicList_view.m
//  MMPopupView
//
//  Created by Ralph Li on 9/7/15.
//  Copyright © 2015 LJC. All rights reserved.
//

#import "Ank_musicList_view.h"
#import "MMPopupDefine.h"
#import "MMPopupCategory.h"
#import <Masonry/Masonry.h>
#import "Music_list_tv_cell.h"
#import "MMPopupWindow.h"

@interface Ank_musicList_view()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong) UIButton *btnCancel;


@end

@implementation Ank_musicList_view


- (instancetype)init
{
    self = [super init];
    
    if ( self )
    {
         [MMPopupWindow sharedWindow].touchWildToHide = YES;
        self.type = MMPopupTypeSheet;
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
        
        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
            make.height.mas_equalTo(ScreenHeight * 0.5 + 50 + Lzy_TabbarSafeBottomMargin);
        }];
        
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"music_list_icon"]];
        [self addSubview:iv];
        [iv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(self).offset(16);
        }];
        
        UILabel *lb = [[UILabel alloc] init];
        [self addSubview:lb];
        [lb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(iv);
            make.left.mas_equalTo(iv.mas_right).offset(8);

        }];
        lb.font = [UIFont systemFontOfSize:18];
        lb.text = @"播放列表";
        
        
        
       
        self.tableView = [UITableView new];
        [self addSubview:self.tableView];
        self.tableView.tableFooterView = [UIView new];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerNib:[UINib nibWithNibName:@"Music_list_tv_cell" bundle:nil] forCellReuseIdentifier:@"Music_list_tv_cell"];
        
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsMake(50, 0, 50 + Lzy_TabbarSafeBottomMargin, 0));
        }];
//        self.tableView.backgroundColor = UIColor.yellowColor;
        
        
        self.btnCancel = [UIButton mm_buttonWithTarget:self action:@selector(actionHide)];
        [self.btnCancel setBackgroundColor:UIColor.whiteColor];
        [self addSubview:self.btnCancel];
        [self.btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.equalTo(self);
            make.bottom.mas_equalTo(self).offset(-Lzy_TabbarSafeBottomMargin);
            make.top.mas_equalTo(self.tableView.mas_bottom);

        }];
        [self.btnCancel setTitle:@"关闭" forState:UIControlStateNormal];
        [self.btnCancel setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        
        UIView *sepV1 = [UIView new];
        [self addSubview:sepV1];
        [sepV1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.tableView).offset(-1);
            make.left.right.mas_equalTo(self);
            make.height.equalTo(@(1));
        }];
        sepV1.backgroundColor = UIColor.groupTableViewBackgroundColor;
        
        
        UIView *sepV2 = [UIView new];
        [self addSubview:sepV2];
        [sepV2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.tableView).offset(1);
            make.left.right.mas_equalTo(self);
            make.height.equalTo(@(1));
        }];
        sepV2.backgroundColor = UIColor.groupTableViewBackgroundColor;
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ank_musicList_view_dataArr_change:) name:@"Ank_musicList_view_dataArr" object:nil];
        
    }
    
    return self;
}
- (void)ank_musicList_view_dataArr_change:(NSNotification *)noti{
    self.dataArr  = [noti.userInfo objectForKey:@"Ank_musicList_view_dataArr"];
    [self.tableView reloadData];
}
- (void)actionHide
{
    [self hide];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Ank_musicList_view_dataArr" object:nil];
     [MMPopupWindow sharedWindow].touchWildToHide = NO;
}


- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    
}

- (void)setCurrentIndex:(NSUInteger)currentIndex{
    NSIndexPath *path = [NSIndexPath indexPathForRow:currentIndex inSection:0];
    [self.tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionTop];
    _currentIndex = currentIndex;

}
- (void)setAudio_execute_status:(NSString *)audio_execute_status{
    _audio_execute_status = audio_execute_status;
}
#pragma mark - Table view
#pragma mark - ================== table view data source delegate ==================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Music_list_tv_cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Music_list_tv_cell"];
    if (!cell) {
        cell = [[Music_list_tv_cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Music_list_tv_cell"];
    }
    App_getMusicListMusic_list *list = self.dataArr[indexPath.row];
    cell.entity = list;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _currentIndex = indexPath.row;
    
    App_getMusicListMusic_list *list = self.dataArr[indexPath.row];
    
    // 样式
    for (App_getMusicListMusic_list *song in self.dataArr) {
        if ([list isEqual:song]) {
            song.isSelected = YES;
        }else{
            song.isSelected = NO;
        }
    }
    [tableView reloadData];
    //
    if (self.selectedRowBlk) {
        self.selectedRowBlk(list);
    }
    
}



@end
